home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_windowmaker.idb / usr / freeware / bin / wm-oldmenu2new.z / wm-oldmenu2new
Encoding:
Text File  |  1999-07-16  |  4.7 KB  |  213 lines

  1. #!/bin/sh
  2. #
  3. # wm-oldmenu2new: script to convert from old-style WindowMaker
  4. # menu file to the new PropertyList style of the WMRootMenu.
  5. #
  6. # Note: ex in all the Linux systems I've used is badly
  7. # broken, except for nex.  perl can be relied on however.
  8. # Re-written to use perl.
  9. #
  10. # Local dependencies:
  11. #   None.
  12. #
  13. # Authors: Luke Kendall, Toby J Sargeant
  14. #
  15. # Copyright waived; no warranty provided.
  16. #
  17.  
  18. GLW=GNUstep/Library/WindowMaker
  19. GD=GNUstep/Defaults
  20. WLW=$HOME/$GLW
  21. WD=$HOME/$GD
  22.  
  23. MYNAME=`basename $0`
  24. USAGE="usage: $MYNAME [menu-file-specifier]
  25.     E.g.    $MYNAME menu.pt
  26.     or        $MYNAME pt
  27.     The default menu if no arguments are given is the English one, 'menu'."
  28.  
  29. #
  30. # Process arguments - work out which language menu we're converting.
  31. # Note that foreign language locales do *not* have the .lang suffix
  32. # attached to the WMRootMenu name.
  33. #
  34. OLD_MENU=menu
  35. NEW_MENU=WMRootMenu
  36. if [ $# = 1 ]
  37. then
  38.     if [ -s "$WLW/menu.$1" ]
  39.     then
  40.     OLD_MENU="menu.$1"
  41.     NEW_MENU="WMRootMenu"
  42.     elif [ -s "$WLW/$1" ]
  43.     then
  44.     OLD_MENU="$1"
  45.     x=`expr "$1" : "menu\.\(.*\)"`
  46.     [ "x$x" != "x" ] && NEW_MENU="WMRootMenu"
  47.     else
  48.     echo "$MYNAME: $WLW/$1 does not exist" >&2
  49.     exit 1
  50.     fi
  51. elif [ $# != 0 ]
  52. then
  53.     echo "$USAGE" >&2
  54.     exit 1
  55. fi
  56.  
  57. #
  58. # For working out what cc is installed
  59. #
  60. which1()
  61. {
  62.     oldpath=$PATH
  63.     PATH=/bin:/usr/bin:/usr/local/bin
  64.  
  65.     IFS=":"
  66.     for j in $oldpath
  67.     do
  68.     test -x $j/$1 && test ! -d $j/$1 && echo $j/$1 && return 0
  69.     done
  70.     IFS=" "
  71.     return 1
  72. }
  73.  
  74. #
  75. # Expand macros if necessary.
  76. # Create a temp copy of the menu file to edit to turn into the new.
  77. #
  78. T=/tmp/wmmenu$$
  79. echo "Converting $GLW/$OLD_MENU --> $GD/$NEW_MENU"
  80. cd $WLW || exit 1
  81. if [ ! -s "$OLD_MENU" ]
  82. then
  83.     echo "$MYNAME: $WLW/$OLD_MENU does not exist" >&2
  84.     exit 1
  85. fi
  86. #
  87. # Always pre-process, to join lines split with \, and to strip comments.
  88. # Not to mention the main purpose, include & process wmmacros if used.
  89. #
  90. set -e
  91. CC=`which1 cc`
  92. [ "x$CC" = "x" ] && CC=`which1 gcc`
  93. [ "x$CC" = "x" ] && "$MYNAME: no cc, gcc found - can't preprocess" >&2 && exit 1
  94.  
  95. #
  96. # Use the "parse as if it's C option" if cc is gcc, because
  97. # newer versions apparently get confused.  Apparently gcc -E does
  98. # not simply run the preprocessor (that's sad).
  99. #
  100. strings "$CC" | grep -l gcc > /dev/null && GCC_FLAGS="-x c"
  101.  
  102. cp $OLD_MENU $T-c
  103. #
  104. # Given the set -e, the exit 1 shouldn't be needed.  But it is, on my NeXT!
  105. #
  106. $CC -E -I. $GCC_FLAGS $T-c > $T+c || exit 1
  107. sed '/^#/d;/^[     ]*$/d' $T+c > $T
  108. rm $T-c $T+c
  109. set +e
  110.  
  111. #
  112. # This is the interesting bit.  Edit the old style menu and
  113. # convert into new style property-list menu.
  114. #
  115. perl - $T <<-'EOF' > $T-p
  116.     $v=chr(22);
  117.     for (<>) {
  118.         push @foo,$_;
  119.     }
  120.     for (@foo) {
  121.         s/\s*$//;
  122.         s/^(\s*)"*(Workspaces*)"*\s\s*(WORKSPACE_MENU)/\1(\2, \3),/;
  123.         s/^(\s*)("[^"]*")\s+MENU/\1($v\n\1\2,/;
  124.         push @foo2,split "\n";
  125.     }
  126.     @foo=();
  127.     for (@foo2) {
  128.         s/^(\s*)"([^"]*)"\s\s*END/\1),/;
  129.         s/^(\s*)"([^"]*)"\s\s*EXEC\s\s*(.*)$/\1($v\n\1"\2",$v\n\1EXEC,$v\n\1"\3"$v\n\1),/;
  130.         push @foo,split "\n";
  131.     }
  132.     @foo2=();
  133.     for (@foo) {
  134.          
  135. s/^(\s*)"([^"]*)"\s\s*OPEN_MENU\s\s*(.*)$/\1($v\n\1"\2",$v\n\1OPEN_MENU,$v\n\1"\3"$v\n\1),/;
  136.         push @foo2,split "\n";
  137.     }
  138.     @foo=();
  139.     for (@foo2) {
  140.         s/^(\s*)([^     ]*)\s\s*MENU/\1($v\n\1"\2",/;
  141.         push @foo,split "\n";
  142.     }
  143.     @foo2=();
  144.     for (@foo) {
  145.         s/^(\s*)([^     ]*)\s\s*END/\1),/;
  146.         s/^(\s*)([^     ]*)\s\s*EXEC\s\s*(.*)$/\1($v\n\1"\2",$v\n\1EXEC,$v\n\1"\3"$v\n\1),/;
  147.         push @foo2,split "\n";
  148.     }
  149.     @foo=();
  150.     for (@foo2) {
  151.         s/^(\s*)([^      
  152. ]*)\s\s*OPEN_MENU\s\s*(.*)$/\1($v\n\1"\2",$v\n\1OPEN_MENU,$v\n\1"\3"$v\n\1),/;
  153.         push @foo,split "\n";
  154.     }
  155.     @foo2=();
  156.     for (@foo) {
  157.         s/ WITH / QQQjjQQQ /;
  158.         s/^(\s*)"([^"]*)"\s\s*([A-Z_][A-Z_]*)$/\1("\2", \3),/;
  159.         s/^(\s*)"([^"]*)"\s+([A-Z_][A-Z_]*)\s\s*(.*)$/\1("\2", \3, \4),/;
  160.         s/"(.*".*)"/JJJqqJJJ\1JJJqqJJJ/;
  161.         /JJJqqJJJ/ && s/"/\\"/g;
  162.         s/JJJqqJJJ/"/g;
  163.         s/ QQQjjQQQ / WITH /;
  164.         print "$_\n";
  165.     }
  166. EOF
  167. mv $T-p $T
  168.  
  169. #
  170. # Now strip off spurious commas from lines like:
  171. #    ),
  172. #    )
  173. # since comma is a property separator, not terminator.  Sigh.
  174. # Also correct for another problem - Linux ex's require the CTRL-V
  175. # above; a real vi/ex doesn't; so we have to strip out any spurious
  176. # CTRL-V characters if we're using a real ex:
  177. #
  178. sed 's///g' $T | awk '
  179.     {
  180.         if (last_line != null)
  181.         {
  182.         if ((last_line ~ /,$/) && ($0 ~ /^[     ]*\)/))
  183.             print substr(last_line, 0, length(last_line)-1)
  184.         else
  185.             print last_line
  186.         }
  187.         last_line = $0
  188.     }
  189.  
  190. END    {
  191.         if (last_line != null)
  192.         {
  193.         if (last_line ~ /,$/)
  194.             print substr(last_line, 0, length(last_line)-1)
  195.         else
  196.             print last_line
  197.         }
  198.     }
  199. ' > $WD/$NEW_MENU.new || exit 1
  200.  
  201. rm $T
  202.  
  203. #
  204. # Now install it.
  205. #
  206. cd $WD
  207. if [ -s $NEW_MENU ]
  208. then
  209.     echo "Preserving $NEW_MENU as $NEW_MENU.sav in $WD"
  210.     mv $NEW_MENU $NEW_MENU.sav
  211. fi
  212. mv $NEW_MENU.new $NEW_MENU && echo "Created new $WD/$NEW_MENU"
  213.